/**


 */
package org.mybatis.generator.codegen.mybatis3.model;

import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
import org.mybatis.generator.codegen.AbstractXmlGenerator;

import java.util.ArrayList;
import java.util.List;

// todo 需要重构 ControllerGenerator ServiceGenerator 中有重复代码
public class ServiceGenerator extends AbstractJavaClientGenerator {

    public ServiceGenerator() {
        super(false);
    }


    public List<Method> getServiceBaseMethod() {

        List<Method> list = new ArrayList<Method>();

        list.add(getDeleteMethod());
        list.add(getInsertMethod());
        list.add(getSelectMethod());
        list.add(getUpdateMethod());

        return list;
    }

    private Method getSelectMethod() {

        StringBuilder sb = new StringBuilder();
        sb.insert(0, "List<").append(introspectedTable.getDomianName()).append(">");

        return getBaseMethod(
                introspectedTable.getSelectByPrimaryKeyStatementId(),
                new FullyQualifiedJavaType(sb.toString())
        );
    }

    private Method getInsertMethod() {
        return getBaseMethod(
                introspectedTable.getInsertSelectiveStatementId(),
                FullyQualifiedJavaType.getStringInstance()
        );
    }

    private Method getUpdateMethod() {

        return getBaseMethod(
                introspectedTable.getUpdateByPrimaryKeySelectiveStatementId(),
                FullyQualifiedJavaType.getStringInstance()
        );
    }


    private Method getDeleteMethod() {

        return getBaseMethod(
                introspectedTable.getDeleteByPrimaryKeyStatementId(),
                FullyQualifiedJavaType.getStringInstance()
                );
    }

    private Method getBaseMethod(
                        String typeName,
                        FullyQualifiedJavaType returnTypeName
                        ) {

        StringBuilder sb = new StringBuilder();
        sb.append(typeName);
        Method method = new Method(sb.toString());

        method.setVisibility(JavaVisibility.DEFAULT);

        sb.setLength(0);
        method.setReturnType(returnTypeName);
        method.addParameter(new Parameter(
                new FullyQualifiedJavaType(introspectedTable.
                        getDomianName()),
                introspectedTable.getDomainFirstNameLowerCase()
        ));

        return method;


    }


    @Override
    public List<CompilationUnit> getCompilationUnits() {

        /**
         * 根据不同的类型生成
         * TopLevelClass 生成class 比如 public class Person
         * Interface     生成interface 比如 public interface Person
         */
        Interface anInterface = new Interface(introspectedTable.getServiceType());

        // class
        interfaceInitClass(anInterface);
        // import
        interfaceInitType(anInterface);
        // method
        List<Method> list = anInterface.getMethods();
        list.addAll(getServiceBaseMethod());

        List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
        answer.add(anInterface);
        return answer;
    }

    /**
     * 初始化TopLevelClass的Class
     *
     * @param javaElement
     */
    private void interfaceInitClass(JavaElement javaElement) {
        javaElement.setVisibility(JavaVisibility.PUBLIC);
    }

    /**
     * 初始化TopLevelClass的Type
     *
     * @param anInterface
     */
    private void interfaceInitType(Interface anInterface) {
        anInterface.addImportedType(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        anInterface.addImportedType(new FullyQualifiedJavaType("java.util.List"));
    }

    @Override
    public AbstractXmlGenerator getMatchedXMLGenerator() {
        return null;
    }
}
